package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Annotation.AdminAccess;
import com.weixin.project.Common.Enum.FileType;
import com.weixin.project.Service.OSSService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Deprecated
@RestController
public class FileController {
    @Value("${document.imagePath}")
    String uploadPath;
    @Autowired
    OSSService ossService;
    @PostMapping("/upload_file")
    public JSONObject uploadImage(@RequestParam("file") MultipartFile file){
        try {
            JSONObject ret = new JSONObject();
            ret.put("url", ossService.uploadImage(file));
            return ResponseUtil.success(ret);
        } catch (IOException e){
            e.printStackTrace();
            return ResponseUtil.error("upload Exception");
        }
    }

    @GetMapping("/images/{fileName:.+}")
    public Resource image(@PathVariable String fileName) {
        return ossService.downloadImage(fileName);
    }

    @GetMapping("/json/{fileName:.+}")
    public Resource json(@PathVariable String fileName) {
        return ossService.downloadJSON(fileName);
    }
}


